// pages/goods/goods.js
Page({
  data: {
    loading: false,
    goodsList: [],
    filteredGoods: [],
    searchValue: '',
    showClear: false,
    historyList: [],
    showHistory: true
  },

  onLoad() {
    this.loadGoodsData()
    this.loadSearchHistory()
  },

  loadGoodsData() {
    this.setData({ loading: true })
    // 模拟API请求
    setTimeout(() => {
      const mockData = [
        // 原有商品数据...
        {
          id: 1,
          specs:'MR',
          name: '夏季新款T恤',
          price: 89.9,
          originalPrice: 129.9,
          imageUrl: 'https://placeholder.pics/300x300/FF9933/FFFFFF/商品1',
        
          sales: 128,
          stock: 999,
          tags: ['热销', '新品']
        },{
          id: 2,
          specs:'MR',
          name: '夏季新款短裤',
          price: 39.9,
          originalPrice: 99.9,
          imageUrl: 'https://placeholder.pics/300x300/FF9933/FFFFFF/商品1',
          sales: 128,
          stock: 999,
          tags: ['热销', '新品']
        },
      ]
      this.setData({
        goodsList: mockData,
        filteredGoods: mockData,
        loading: false
      })
    }, 800)
  },

  previewImage(e) {
    const current = e.currentTarget.dataset.url;
    const urls = this.data.goodsList.map(item => item.imageUrl);
    
    wx.previewImage({
      current,
      urls
    });
  },

  loadSearchHistory() {
    const history = wx.getStorageSync('searchHistory') || []
    this.setData({ historyList: history })
  },

  onInputSearch(e) {
    const value = e.detail.value.trim()
    this.setData({
      searchValue: value,
      showClear: value.length > 0
    })
    
    if (value) {
      this.filterGoods(value)
      this.setData({ showHistory: false })
    } else {
      this.setData({ 
        filteredGoods: this.data.goodsList,
        showHistory: true 
      })
    }
  },

  filterGoods(keyword) {
    const filtered = this.data.goodsList.filter(item => 
      item.name.includes(keyword) || 
      (item.tags && item.tags.some(tag => tag.includes(keyword)))
    )
    this.setData({ filteredGoods: filtered })
  },

  onSearch() {
    if (!this.data.searchValue) return
    
    // 保存搜索历史
    const newHistory = [...new Set([
      this.data.searchValue, 
      ...this.data.historyList
    ])].slice(0, 8)
    
    wx.setStorageSync('searchHistory', newHistory)
    this.setData({ 
      historyList: newHistory,
      showHistory: false 
    })
  },

  onClear() {
    this.setData({
      searchValue: '',
      showClear: false,
      filteredGoods: this.data.goodsList,
      showHistory: true
    })
  },

  onHistoryItemTap(e) {
    const keyword = e.currentTarget.dataset.keyword
    this.setData({
      searchValue: keyword,
      showClear: true
    })
    this.filterGoods(keyword)
    this.setData({ showHistory: false })
  },

  clearHistory() {
    wx.removeStorageSync('searchHistory')
    this.setData({ historyList: [] })
  },

  addToCart(e) {
    const product = e.currentTarget.dataset.item
    getApp().addToCart(product)
    wx.showToast({
      title: '已加入购物车',
      icon: 'success'
    })
  }
})